// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: Settings.Java 
// AUTHOR: CDT Riley Mckenna 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for the settings Tab of Nullwall
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////
package prototype.revision;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;

public class GeneralSettings extends GenericActivity {

	//private ToggleButton det;
	private ToggleButton alerts;
	private ToggleButton blockAll;

	private PermissionsManager pm = new PermissionsManager();
	private FileOperations fo = new FileOperations(this);
	private Blocker blocker = new Blocker(fo);
	private String name = "myPrefs";
	private boolean statusBar = true;
	boolean detection;
	SharedPreferences settings;
	SharedPreferences.Editor editor;
	private Activity thisActivity = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingspage);
		builder();
		settings = getSharedPreferences(name, Activity.MODE_PRIVATE);
		editor = settings.edit();
	}

	@Override
	public void onPause(Bundle savedInstanceState) {
		super.onPause(savedInstanceState);
		saveBool(blockAll, "block");
		//saveBool(det, "det");
		saveBool(alerts, "alert");

	}

	@Override
	public void onStop(Bundle savedInstanceState) {
		super.onStop(savedInstanceState);
		saveBool(blockAll, "block");
		//saveBool(det, "det");
		saveBool(alerts, "alert");

	}

	@Override
	public void onResume() {
		super.onResume();
		setToggle(blockAll, "block");
	//	setToggle(det, "det");
		setToggle(alerts, "alert");

	}

	public void builder() {
		//det = (ToggleButton) findViewById(R.id.detect);
		alerts = (ToggleButton) findViewById(R.id.alert);
		blockAll = (ToggleButton) findViewById(R.id.blockall);

		// listeners
		alerts.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (alerts.isChecked()) {
					setStatusBarBool(true);
				} else {
					setStatusBarBool(false);
				}
				saveBool(alerts, "alert");
			}

		});
		blockAll.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (blockAll.isChecked()) {
					if (statusBar == true) {
						statusNotificationBlocker("Internet Permissions", "",
								"Blocked", thisActivity);
					}
					blocker.blockall(pm.getINTERNET());

				} else {
					if (statusBar == true) {
						statusNotificationBlocker("Internet Permissions", "",
								"Unblocked", thisActivity);
					}
					blocker.unblockall(pm.getINTERNET());
					//pm.block_all(false, pm.getINTERNET());
				}
				saveBool(blockAll, "block");
			}

		});
	}

	public void setToggle(ToggleButton toggle, String name) {
		boolean tg = settings.getBoolean(name, true); // default is true
		if (tg == true) {
			toggle.setChecked(true);
			//return true;
		} else {
			toggle.setChecked(false);
			//return false;
		}
	}
	
	public boolean getBool(ToggleButton toggle, String name) {
		boolean tg = settings.getBoolean(name, true); // default is true
		if (tg == true) {
			return true;
		} else {
			return false;
		}
	}

	public void saveBool(ToggleButton toggle, String name) {
		if ((toggle.isChecked())) {
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(name, true); // value to store
			editor.commit();
		} else {
			SharedPreferences.Editor editor = settings.edit();
			editor.putBoolean(name, false); // value to store
			editor.commit();
		}

		;
	}

	public boolean getStatusBarBool() {
		return statusBar;
	}

	public void setStatusBarBool(boolean statusBar) {
		this.statusBar = statusBar;
	}
	
	public boolean isDetectionOn() {
		return detection;
	}

	public void setDetection(boolean detection) {
		this.detection = detection;
	}

}
